package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author timiv
 */
@Service
public class PaymentServiceImpl implements PaymentService {
    @Override
    public String paymentInfo_ok(Integer id) {
        return "线程池 "+Thread.currentThread().getName() + " paymentInfo_ok,id:" + id + " 哈哈";
    }

    /**
     * 服务超时、运行异常降级配置
     * @param id
     * @return
     */
    @Override
    @HystrixCommand(fallbackMethod = "paymentInfo_handle",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
    })
    public String paymentInfo_timeOut(Integer id) {
        //int i = 0 /0;
        int sleep = 3;
        try {
            TimeUnit.SECONDS.sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池 "+Thread.currentThread().getName() + " paymentInfo_timeOut,id:" + id + " 休眠" + sleep + "秒";
    }

    public String paymentInfo_handle(Integer id){
        return "线程池 "+Thread.currentThread().getName() + " 系统繁忙，请稍后再用,id:" + id + "  /(ㄒoㄒ)/~~";
    }
}
